package com.android.lovingpet.adopt.host.behost.view;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.android.lovingpet.Application;
import com.android.lovingpet.R;
import com.android.lovingpet.adopt.host.behost.PetExp;

import java.util.List;

/**
 * Created by ikata on 2018/5/7.
 */

public class PetRvAdapter extends RecyclerView.Adapter<PetRvAdapter.ViewHolder>{

    public List<PetExp> getList() {
        return list;
    }

    private List<PetExp> list;
    public PetRvAdapter(List<PetExp> list)
    {
        this.list=list;
    }


    public void add()
    {
       list.add(list.size(),new PetExp());
       notifyItemInserted(list.size());
    }
    public void delete(int position)
    {
        list.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_pet_experience,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        int num=list.get(position).getCount();
        holder.iv_delete.setOnClickListener(v -> {
            delete(position);
        });
        holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                holder.spinner.setSelection(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        holder.iv_increase.setOnClickListener(v -> {
           list.get(position).setCount(num+1);
            holder.et_num.setText(String.valueOf(num+1));
           notifyItemChanged(position);
        });
        holder.iv_decrease.setOnClickListener(v -> {
            if (num>=1) {
                list.get(position).setCount(num - 1);
                holder.et_num.setText(String.valueOf(num-1));
                notifyItemChanged(position);
            }
        });

        String[] mItems = Application.getContext().getResources().getStringArray(R.array.petType);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(Application.getContext(),R.layout.support_simple_spinner_dropdown_item,mItems);
        holder.spinner.setAdapter(adapter);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder
    {
        ImageView iv_increase;
        ImageView iv_decrease;
        ImageView iv_delete;
        EditText et_num;
        Spinner spinner;
        public ViewHolder(View itemView)
        {
            super(itemView);

            iv_delete=itemView.findViewById(R.id.iv_delete);
            iv_decrease=itemView.findViewById(R.id.iv_decrease);
            iv_increase=itemView.findViewById(R.id.iv_increase);
            et_num=itemView.findViewById(R.id.iv_num);
            spinner=itemView.findViewById(R.id.sp_type);
        }
    }
}
